package com.layblog.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.layblog.common.lang.Result;
import com.layblog.entity.Post;
import com.layblog.entity.UserCollection;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 收藏
 *
 * @Author 林必昭
 * @Date 2021/9/4 10:02
 */

@Controller
@RequestMapping("/center")
public class UserCollectionController extends BaseController {

    /**
     * 跳转收藏页面
     *
     * @return
     */
    @GetMapping("/collection")
    public String collection() {

        Page page = getPage();
        QueryWrapper queryWrapper = new QueryWrapper<>().eq("u.user_id", getProfileId()).orderByAsc("u.created");
        IPage<Post> pageData = userCollectionService.paging(page, queryWrapper);

        req.setAttribute("pageData", pageData);

        return "center/collection";
    }

    /**
     * <p>
     * 显示收藏文章的数量,调用js的异步渲染jie.js#125
     * <p>
     * 收藏功能实现很简单，就是从UserCollection中查询是否有记录就行，如果有表明收藏了，
     * js会渲染出取消收藏的按钮，如果没有记录，就会渲染出收藏的按钮。
     * </p>
     *
     * @param pid
     * @return
     */
    @ResponseBody
    @PostMapping("/collection/find/")
    public Result collectionFind(Long pid) {

        int count = userCollectionService.count(new QueryWrapper<UserCollection>()
                .eq("post_id", pid)
                .eq("user_id", getProfileId()));
        //此方法将一个或多个键值对加入到一个新建的Map
        return Result.succ(MapUtil.of("collection", count > 0));
    }


    /**
     * 收藏
     *
     * @param pid
     * @return
     */
    @ResponseBody
    @PostMapping("/collection/add/")
    public Result collectionAdd(Long pid) {
        Post post = postService.getById(pid);
        Assert.isTrue(post != null, "该帖子已被删除");

        //先判断是否已经收藏过了
        int count = userCollectionService.count(new QueryWrapper<UserCollection>()
                .eq("post_id", pid)
                .eq("user_id", getProfileId()));
        if (count > 0) {
            return Result.fail("你已经收藏");
        }
        UserCollection collection = new UserCollection();
        collection.setUserId(getProfileId());
        collection.setPostUserId(post.getUserId());
        collection.setPostId(pid);
        collection.setCreated(new Date());
        collection.setModified(new Date());

        userCollectionService.save(collection);
        return Result.succ(MapUtil.of("collection", true));   //成功收藏返回
    }

    /**
     * 取消收藏：删除一条记录即可
     *
     * @param pid
     * @return
     */
    @ResponseBody
    @PostMapping("/collection/remove/")
    public Result collectionRemove(Long pid) {
        Post post = postService.getById(pid);
        Assert.isTrue(post != null, "该帖子已被删除");

        boolean hasRemove = userCollectionService.remove(new QueryWrapper<UserCollection>()
                .eq("post_id", pid)
                .eq("user_id", getProfileId()));
        return Result.succ(hasRemove);
    }
}
